'''
This example writes a list of players with guid with the ranking scores


'''

from tremapi import TremListener
from pageRank import *

class TremRank(TremListener):
	def __init__(self):
		self.guids={}
		self.matrix=[]
		self.ShutdownGame()
		self.pos2guid=[]
		self.guid2name={}
		self.killdeads=[]
	def say(self, msg):
		pass
		
	def ClientConnect(self, msg):
		self.player_names[msg.slot]=msg.name
		self.player_guids[msg.slot]=msg.guid
		self._addGuid(msg.guid)
		if not self.guid2name.has_key(msg.guid):
			self.guid2name[msg.guid]=[]
		name=unformat(msg.name)
		if not name in self.guid2name[msg.guid]:
			self.guid2name[msg.guid].append(name)
		
	def _getGuidBySlot(self, s):
		if(self.player_guids.has_key(s) and
			self.player_guids[s]!='XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'):
				return self.player_guids[s]
		else:
			return None
	def _addGuid(self, guid):
		if(not self.guids.has_key(guid)):
				p=len(self.guids)
				self.matrix.append([])
				self.pos2guid.append(0)
				self.guids[guid]=p
				self.pos2guid[p]=guid
				self.killdeads.append([0,0])
				

		
	def _getIndexByGuid(self, guid):
		return self.guids[guid]
		
	def Kill(self, m):
		#print ("%d, %d, %d - %s" % (m.slot, m.victim, m.weapon, m.textmsg))
		s=self._getGuidBySlot(m.slot)
		v=self._getGuidBySlot(m.victim)
		if s!=None and v!=None and s!=v:
			#print ("%s => %s" % (v,s))
			s=self._getIndexByGuid(s)
			v=self._getIndexByGuid(v)
			self.matrix[v].append(s)
			self.killdeads[s][0]+=1
			self.killdeads[v][1]+=1
			#if(not self.matrix[v].has_key(s)):
			#	self.matrix[v][s]=1
			#else:
			#	self.matrix[v][s]+=1
		
	def ClientDisconnect(self, m):
		if(self.player_names.has_key(m.slot)):
			del self.player_names[m.slot]
			del self.player_guids[m.slot]
		
	def InitGame(self, msg):
		pass
		
	def Exit(self, msg):
		pass
			
	def ShutdownGame(self, msg=None):
		self.player_names={}
		self.player_guids={}
		
			
	def ClientRename(self, msg):
		self.player_names[msg.slot]=msg.name
		
import os
from tremapi import TremParserLog
#from tremrank import TremRank
 
def unformat(s):
	o='';
	i=0
	while i<len(s):
		if s[i]=='^':
			i+=2
		else:
			o+=s[i]
			i+=1
	return o
	
tp=TremParserLog("games.log")
tremrank=TremRank()
tp.addListener(tremrank)
tp.run()

matrix=tremrank.matrix
#print ("Total de guids: %d\n" % len(matrix))
#print ("Diagrama");
#print (matrix)
#print ("Ranking:\n")

#m=[list(range(1,len(matrix))),]
#for i in range(len(matrix)):
#	m.append([])
#	for j in matrix[i]:
#		m[i+1].append(j+1)
#	m[i+1].append(0)
for i in range(len(matrix)):
	#if len(matrix[i])==0:
	matrix[i].append(i)	
#print ("Diferencia")
#print (m)
deads=[]
totaldeads=0
for i in range(len(tremrank.killdeads)):
	tremrank.killdeads[i][0]+=1.0
	tremrank.killdeads[i][1]+=1.0
	if(tremrank.killdeads[i][1]>10):
		deads.append(tremrank.killdeads[i][0])
	else:
		deads.append(0)
		
	if tremrank.killdeads[i][0]>totaldeads:
			totaldeads=tremrank.killdeads[i][0]
	#totaldeads+=tremrank.killdeads[i][0]

e=1
deads=array(deads)
deads/=deads.max()
deads*=e
deads+=1-e
#print deads
	
rank=pageRank(matrix, alpha=1, 	convergence = 0.01, checkSteps = 10, deads=deads)
# normalizamos ranking
rank/=rank.max()
'''
for i in range(len(rank)):
	if tremrank.killdeads[i][1]==0:
		rank[i]=0 #anulamos invalidos
	else:
		rank[i]=rank[i]*tremrank.killdeads[i][0]/tremrank.killdeads[i][1]
'''	
srank=(rank*-1+1).argsort()

print ("[table]")
print ("[tr][td]Pos[/td][td]k/f(ratio)[/td][td]rank[/td][td]victim rank avg[/td][td]names[/td][/tr]")
for j in range(50):
	i=srank[j]
	guid=tremrank.pos2guid[i]
	
	vrank=0
	for v in matrix[i]:
		vrank+=rank[v]
	vrank/=len(matrix[i])
	
	print ("[tr][td]%-3d[/td][td]%d/%d(%d)[/td][td]%f[/td][td]%f[/td][td]%s[/td][/tr]" % (j+1, tremrank.killdeads[i][0], tremrank.killdeads[i][1], 100*tremrank.killdeads[i][0]/tremrank.killdeads[i][1], rank[i], vrank, ', '.join(tremrank.guid2name[guid])))

print ("[/table]")
